﻿//
// Copyright 2024 Exhunter Technology Co.Ltd2.
//

using System;
using log4net;
using log4net.Config;

namespace X18.Framework.Core.Logs
{
    /// <summary>
    /// 日志工具
    /// </summary>
    public class Logger
    {
        private static ILog logger;

        /// <summary>
        /// 初始化
        /// </summary>
        public static void Init()
        {
            XmlConfigurator.Configure();
        }

        internal static ILog Log
        {
            get
            {
                if (logger == null)
                {
                    lock (typeof(Logger))
                    {
                        logger = LogManager.GetLogger(typeof(Logger));
                    }
                }

                return logger;
            }
        }

        /// <summary>
        /// 记录调试信息
        /// </summary>
        /// <param name="logger">日志标志</param>
        /// <param name="message">日志信息</param>
        public static void Debug(string logger, string message)
        {
            if (Log.IsDebugEnabled)
            {
                Log.Debug(GetLogMessage(logger, message));
            }
        }

        /// <summary>
        /// 记录提示信息
        /// </summary>
        /// <param name="logger">日志标志</param>
        /// <param name="message">日志信息</param>
        public static void Info(string logger, string message)
        {
            if (Log.IsInfoEnabled)
            {
                Log.Info(GetLogMessage(logger, message));
            }
        }

        /// <summary>
        /// 记录提示信息
        /// </summary>
        /// <param name="logger">日志标志</param>
        /// <param name="message">日志信息</param>
        public static void Error(string logger, string message, Exception ex = null)
        {
            if (Log.IsErrorEnabled)
            {
                Log.Error(GetLogMessage(logger, message), ex);
            }
        }

        /// <summary>
        /// 拼装日志标志和日志信息
        /// </summary>
        /// <param name="logger">日志标志</param>
        /// <param name="message">日志信息</param>
        /// <returns></returns>
        private static object GetLogMessage(string logger, string message)
        {
            return logger + " - " + message;
        }
    }
}
